<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class AuthController extends XcxController
{
    public function __construct()
    {
//        除login外都需要进行验证
        $this->middleware('auth:api', ['except' => ['login']]);
    }

//    登录需要获取token
    public function login()
    {
        $credentials = request(['email', 'password']);
        if (!$token = auth('api')->attempt($credentials)) {

            return $this->response->errorUnauthorized('账号或密码错误');
        }

        return $this->respondWithToken($token);
    }

    public function me() {
        return response()->json(auth('api')->user());
    }

    public function logout(){
        auth('api')->logout();
        return response()->json(['message' => '退出成功']);
    }

    public function refresh(){
        return $this->respondWithToken(auth('api')->refresh());
    }

    protected function respondWithToken($token){
        return response()->json([
            'access_token' => $token,
            'token_type'   => 'bearer',
            'expires_in'   => auth('api')->factory()->getTTL() * 60,                ]);
    }

}
